package com.bwbear.db.splitdb.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @author 张照威
 * @date 2020/8/17
 * @mail 865533614@qq.com
 * @describe
 */
public abstract class DefaultFieldValueHandler implements MetaObjectHandler {

    private static final String CREATE_BY = "createBy";
    private static final String CREATE_DATE = "createDate";

    public void handle(String name, MetaObject metaObject, Object target) {
        Object o = getFieldValByName(name, metaObject);
        if (o == null) {
            setFieldValByName(name, target, metaObject);
        }
    }


    /**
     * 插入操作时触发
     * @param metaObject
     * handle(CREATE_BY, metaObject, userId);
     * handle(CREATE_DATE, metaObject, new Timestamp(new Date().getTime()));
     */
    @Override
    public abstract void insertFill(MetaObject metaObject);

    /**
     * 变更操作时触发
     * @param metaObject
     * handle(LAST_UPDATE_BY, metaObject, loginUser.getUserId());
     * handle(LAST_UPDATE_DATE, metaObject, new Timestamp(new Date().getTime()));
     */
    @Override
    public abstract void updateFill(MetaObject metaObject);
}
